﻿using UnityEngine;
using System.Collections;

// aplaczek 25.11.2014
public class AutoMoving : MonoBehaviour
{
    // parameters
    public Transform inStartPoint;
    public Transform inEndPoint;
    public float inSpeed = 1.0f;

    static private Vector3 rightTurn = new Vector3(0.0f, 0.0f, 0.0f);
    static private Vector3 leftTurn = new Vector3(0.0f, 180.0f, 0.0f);

    // Update is called once per frame
    void Update()
    {
        // [-1;1]
        float theta = Mathf.Sin(Time.time * inSpeed);

        // [-1;1] -> [0;1]
        theta = (theta + 1.0f) / 2.0f;

        // interpolate position
        Vector3 newPosition = Vector3.Lerp(inStartPoint.position, inEndPoint.position, theta);
        if (gameObject.layer == 9) // Enemy
        {
            if (newPosition.x > transform.position.x)
            {
                transform.localEulerAngles = rightTurn;
            }
            else if (newPosition.x < transform.position.x)
            {
                transform.localEulerAngles = leftTurn;
            }
        }


        transform.position = newPosition;
    }
}
